//验证手机号正则
var _checkMobile = function(sMobile) {
	if(!(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(sMobile))) {
		return false;
	} else {
		return true;
	}
}

//转换时间戳
var _date = function(timestamp) {
	if(!timestamp) {
		return "";
	}
	var date;
	//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	if(String(timestamp).length == 10) {
		date = new Date(Number(timestamp) * 1000);
	}
	if(String(timestamp).length == 13) {
		date = new Date(Number(timestamp));
	}
	var Y = date.getFullYear() + "-";
	var M =
		(date.getMonth() + 1 < 10 ?
			"0" + (date.getMonth() + 1) :
			date.getMonth() + 1) + "-";
	var D = date.getDate() + " ";
	var h = date.getHours() + ":";
	if(date.getHours() < 10) {
		h = '0' + date.getHours() + ":";
	}
	var m = date.getMinutes() + ":";
	if(date.getMinutes() < 10) {
		m = '0' + date.getMinutes() + ":";
	}
	var s = date.getSeconds();
	if(date.getSeconds() < 10) {
		s = '0' + date.getSeconds();
	}
	return Y + M + D + h + m + s;
};
//钱分转换为元
var _money = function(val) {
	var judge = String(val);
	if(judge == "undefined" || judge == "null") {
		return 0.0;
	}
	var priceStr = String(parseFloat(parseInt(val) / 100));
	var index = priceStr.indexOf(".");
	if(index != -1) {
		var handle = priceStr.slice(index + 1);
		if(handle.length == 1) {
			priceStr = priceStr + "0";
		}
	} else {
		priceStr = priceStr + ".00";
	}
	return priceStr;
};
/*
	content:密码错误(提示内容)
	position:center(提示的垂直位置)
*/
var _toast = function(content, position) {
	if(!position) {
		position = 'center';
	}
	plus.nativeUI.toast(content, {
		verticalAlign: position
	});
};

// 复制到剪贴板
var _copy = function(text) {
	var osName = plus.os.name;
	if(plus.os.name == "Android") {
		var Context = plus.android.importClass("android.content.Context");
		var main = plus.android.runtimeMainActivity();
		var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
		plus.android.invoke(clip, "setText", text);
	} else if(osName == "iOS") {
		var UIPasteboard = plus.ios.importClass("UIPasteboard");
		var generalPasteboard = UIPasteboard.generalPasteboard();
		// 设置/获取文本内容:
		generalPasteboard.setValueforPasteboardType(text, "public.utf8-plain-text");
	}
};

//截取6位
var _toFixed = function(str1, num) {
	var str = String(str1);

	function hanZeo(z) {
		var zeo = '';
		for(var i = 0; i < z; i++) {
			zeo += '0';
		}
		return zeo;
	}
	var arr = str.split('.');
	if(arr[1]) {
		if(arr[1].length >= num) {
			return arr[0] + '.' + arr[1].slice(0, num);
		} else {
			return arr[0] + '.' + arr[1] + hanZeo(num - arr[1].length);
		}
	} else {
		return str + '.' + hanZeo(num);
	}
};

//判断输入的是小数或整数
var _ifNum = function(val, type) {
	// 返回true:满足   false:不满足
	// type:int/float
	if(!parseFloat(val)) {
		return false;
	}
	var str = String(val);
	var point = 0;
	var off = true;
	for(var i = 0; i < str.length; i++) {
		if(isNaN(Number(str[i]))) {
			//不是数字
			if(str[i] == '.') {
				point++;
			} else {
				off = false;
				break;
			}
		}
	}
	if(type == 'int') {
		(off && !point) ? '' : off = false;
	}
	if(type == 'float') {
		(off && point <= 1 && str[str.length - 1] != '.' && str[0] != '.') ? '' : off = false;
	}
	return off;
};

var _bas64Post = function(url, json, cal) {
	function handleBase64(data) {
		console.log('原没加密的json========');
		console.log(JSON.stringify(data));
		var base = new Base64();
		var list = {};
		for(var key in data) {
			var val = null;
			if(typeof data[key] == 'object' || typeof data[key] == 'number') {
				val = JSON.stringify(data[key]);
			} else {
				val = data[key];
			}
			list[base.encode(key)] = base.encode(val);
		}
		return list;
	}
	_post(url, handleBase64(json), cal);
};

var _post = function(url, json, cal) {
	console.log('url++++++' + url);
	console.log('post参数+++++++++');
	console.log(JSON.stringify(json));
	var obj = new XMLHttpRequest();
	obj.open('post', url, true);
	obj.onreadystatechange = function() {
		if(obj.readyState == 4 && obj.status == 200 || obj.status == 304) {
			switch(obj.responseText) {
				case '本接口需要传递参数':
				case '本接口需要传递用户参数':
				case 'session无效':
					_toast(obj.responseText);
					setTimeout(function() {
						plus.webview.currentWebview().close('none');
					}, 1000);
					break;
				default:
					var ret = JSON.parse(obj.responseText);
					console.log('post返回的结果++++++');
					console.log(JSON.stringify(ret));
					cal(ret);
			}
		}
	};
	obj.send(JSON.stringify(json));
}

//关闭本页面
var _close = function() {
	plus.webview.currentWebview().close();
};

//打开页面
var _openHref = function(json) {
	var animate = json.animate ? json.animate : 'slide-in-right';
	var op = {
		'backButtonAutoControl': 'close', //返回是关闭当前页
		statusbar: {
			background: json.bg ? json.bg : '#ffffff'
		},
		popGesture: 'close'
	};
	var web = plus.webview.getWebviewById(json.id);
	if(web) {
		web.show();
	} else {
		app.$data.jiazai = true;
		web = plus.webview.create(json.url, json.id, op);
		web.addEventListener("loaded", function() {
			setTimeout(function() {
				app.$data.jiazai = false;
				web.show(animate);
			}, 200);
		}, false);
	}
	return web;
}

//在首页打开页面
var _indexOpenHref = function(json) {
	var animate = json.animate ? json.animate : 'slide-in-right';
	var op = {
		'backButtonAutoControl': 'close', //返回是关闭当前页
		statusbar: {
			background: json.bg ? json.bg : '#ffffff'
		},
		popGesture: 'close'
	};
	var web = plus.webview.getWebviewById(json.id);
	if(web) {
		web.show();
	} else {
		app.$data.jiazai = true;
		var bottom = plus.webview.getWebviewById('bottom');
		bottom.evalJS('openJia()');
		web = plus.webview.create(json.url, json.id, op);
		web.addEventListener("loaded", function() {
			setTimeout(function() {
				app.$data.jiazai = false;
				bottom.evalJS('closeJia()');
				web.show(animate);
			}, 200);
		}, false);
	}
	return web;
};

//首頁打開加載動畫
var _openJiazai = function() {
	app.$data.jiazai = true;
	plus.webview.getWebviewById('bottom').evalJS('openJia()');
};

//首頁關閉加載動畫
var _closeJiazai = function() {
	app.$data.jiazai = false;
	plus.webview.getWebviewById('bottom').evalJS('closeJia()');

};